package com.liustar.common.response;

import com.alibaba.fastjson.JSON;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.Serializable;
import java.util.HashMap;


/**
 * @author Leo
 * @date 2017/3/14
 */
public final class ResultMessage implements Serializable {

    private final static Logger logger = LogManager.getLogger(ResultMessage.class);

    private static final long serialVersionUID = -4799509005682617335L;

    private String resultCode;
    private String resultMsg;

    private HashMap data = new HashMap();

    public static ResultMessage newSuccess() {
        return new ResultMessage(ResultConstant.SUCCESS);
    }

    public static ResultMessage newFail() {
        return new ResultMessage(ResultConstant.FAIL);
    }

    public static ResultMessage newError() {
        return new ResultMessage(ResultConstant.ERROR);
    }

    public ResultMessage(String code, String msg) {
        this.resultCode = code;
        this.resultMsg = msg;
    }

    public ResultMessage(ResultConstant rc) {
        this.resultCode = rc.getIndex();
        this.resultMsg = rc.getValue();
    }

    public ResultMessage setSuccess(String format, Object... vars) {
        this.resultCode = ResultConstant.SUCCESS.getIndex();
        this.resultMsg = String.format(format, vars);
        logger.debug(this.resultMsg);
        return this;
    }

    public ResultMessage setFail(String format, Object... vars) {
        this.resultCode = ResultConstant.FAIL.getIndex();
        this.resultMsg = String.format(format, vars);
        logger.debug(this.resultMsg);
        return this;
    }

    public ResultMessage setError(String format, Object... vars) {
        this.resultCode = ResultConstant.ERROR.getIndex();
        this.resultMsg = String.format(format, vars);
        logger.debug(this.resultMsg);
        return this;
    }

    public ResultMessage setSuccess() {
        this.resultCode = ResultConstant.SUCCESS.getIndex();
        this.resultMsg = ResultConstant.SUCCESS.getValue();
        logger.debug(this.resultMsg);
        return this;
    }

    public ResultMessage setFail() {
        this.resultCode = ResultConstant.FAIL.getIndex();
        this.resultMsg = ResultConstant.FAIL.getValue();
        logger.debug(this.resultMsg);
        return this;
    }

    public ResultMessage setError() {
        this.resultCode = ResultConstant.ERROR.getIndex();
        this.resultMsg = ResultConstant.ERROR.getValue();
        logger.debug(this.resultMsg);
        return this;
    }

    public ResultMessage setCodeAndMsg(ResultConstant rc) {
        this.resultCode = rc.getIndex();
        this.resultMsg = rc.getValue();
        logger.debug(this.resultMsg);
        return this;
    }

    public String getResultCode() {
        return resultCode;
    }

    public ResultMessage setResultCode(String resultCode) {
        this.resultCode = resultCode;
        return this;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public ResultMessage setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
        logger.debug(resultMsg);
        return this;
    }

    public ResultMessage setResultMsg(String format, Object vars) {
        this.resultMsg = String.format(format, vars);
        logger.debug(this.resultMsg);
        return this;
    }

    public HashMap getData() {
        return data;
    }

    public ResultMessage putData(String key, Object value) {
        try {
            this.data.put(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return this;
        }
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
